//
//  RightSideViewController.m
//  YouthOlympic2014
//
//  Created by xiangpaopao on 14-6-30.
//  Copyright (c) 2014年 mitian. All rights reserved.
//

#import "RightSideViewController.h"
#import "RightSideCell.h"
#import "UIViewController+MMDrawerController.h"
#import "MsgDetailViewController.h"
#import "BaseNavViewController.h"
#import "NotificationCenter.h"
#import "MessageService.h"

@interface RightSideViewController ()

@property (nonatomic, strong) NSArray* messages;

@property (nonatomic, strong)IBOutlet UIView *hdView;

@end

@implementation RightSideViewController
@synthesize messages;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.messages = [[[MessageService alloc]init] getAllMessages];
    
    if ([self.messages count]==0) {
        self.listView.tableHeaderView = self.hdView;
    }else{
        self.listView.tableHeaderView = nil;
    }
    
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updateInfo:)
                                                 name:showMsgInfoNotification
                                               object:nil];
}

-(void)updateInfo:(NSNotification *)notes
{
    self.messages = [[[MessageService alloc]init] getAllMessages];
    
    if ([self.messages count]==0) {
        self.listView.tableHeaderView = self.hdView;
    }else{
        self.listView.tableHeaderView = nil;
    }
    
    [self.listView reloadData];
    NSLog(@"reload listview data");
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - TableView delegate & datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 75.0;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    return self.messages.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"rightSideCell";
    RightSideCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
        cell = [topLevelObjects objectAtIndex:0];
    }
    Message* message = (Message*)self.messages[indexPath.row];
    
    [cell configureWithTitle:message.title
                    subTitle:message.content];
    
    NSLog(@"[message.isRead boolValue] %hhd",[message.isRead boolValue]);
    
    [cell setRead:message.isRead];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    BaseNavViewController *msgCtrl = [self.storyboard instantiateViewControllerWithIdentifier:@"msgCtrl"];

    /*
    NSMutableDictionary *message = [NSMutableDictionary dictionaryWithCapacity:5];
    [message setObject:@"赛事开始提醒通知" forKey:@"title"];
    [message setObject:@"2014-11-11 11:11" forKey:@"date"];
    [message setObject:@"欢迎来到2014年第二届夏季青年奥林匹克运动会欢迎来到2014年第二届夏季青年奥林匹克运动会欢迎来到2014年第二届夏季青年奥林匹克运动会" forKey:@"content"];
    
    */
    Message* messageObj = self.messages[indexPath.row];
    
    
    if (messageObj.isRead.intValue == 0) {
        [[[MessageService alloc] init] markMessageAsRead:messageObj.cid];
        RightSideCell *cell = (RightSideCell*)[self.listView cellForRowAtIndexPath:indexPath];
        
        [cell setRead:[NSNumber numberWithInt:1]];
    }
    
    NSMutableDictionary* message = [NSMutableDictionary dictionaryWithCapacity:5];
    message[@"title"] = messageObj.title;
    message[@"content"] = messageObj.content;
    message[@"date"] = messageObj.date;
    
    
    
    msgCtrl.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:msgCtrl animated:YES completion:^{
        [[NSNotificationCenter defaultCenter] postNotificationName:showMsgInCenterNotification
                                                            object:nil
                                                          userInfo:message];
    }];
    
    NSLog(@"didSelectRowAtIndexPath");
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

//-(IBAction)settingAction:(id)sender
//{
//    BaseNavViewController *settingCtrl = [self.storyboard instantiateViewControllerWithIdentifier:@"settingCtrl"];
//    [self.mm_drawerController setCenterViewController:settingCtrl
//                                   withCloseAnimation:YES
//                                           completion:^(BOOL isfinished){
//                                               
//                                           }];
//    
//}


@end
